package cn.qiyu5522.tk_ems.controller.admin;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.tk_ems.entity.Menu;
import cn.qiyu5522.tk_ems.entity.dto.MenuDTO;
import cn.qiyu5522.tk_ems.myanotation.WebLog;
import cn.qiyu5522.tk_ems.result.R;
import cn.qiyu5522.tk_ems.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2023-02-27
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("admin/menu")
public class MenuController {

    @Resource
    private MenuService menuService;


//    @WebLog(description = "分页条件查询菜单列表")
//    @ApiOperation("菜单列表查询")
//    @ApiImplicitParam(name = "queryParamDTO", value = "条件查询对象", required = true,dataType ="QueryParamDTO" ,dataTypeClass = QueryParamDTO.class)
//    @PostMapping("/query")
//    public R query(@Validated(QueryParamDTO.Query.class) @RequestBody QueryParamDTO<MenuDTO> queryParamDTO) {
//        return menuService.queryMenuListByCondition(queryParamDTO);
//    }

    @WebLog(description = "获取菜单树结构")
    @ApiOperation("获取菜单树结构")
    @GetMapping("/queryMenuTree")
    public R queryMenuTree() {
        List<String> roleList = StpUtil.getRoleList();
        boolean admin = roleList.stream().anyMatch(s -> s.equals("admin"));
        return admin?  menuService.getMenuTree(): menuService.queryMenuListByRole();
    }


    @WebLog(description = "菜单新增--批量")
    @ApiOperation("菜单新增")
    @ApiImplicitParam(name = "MenuDTOs", value = "菜单新增", required = true,dataType = "List",dataTypeClass = List.class)
    @PostMapping("/add")
    public R add(@Validated(Menu.Insert.class) @RequestBody List<MenuDTO> MenuDTOs) {
        return menuService.saveMore(MenuDTOs);
    }
    @WebLog(description = "菜单编辑")
    @ApiOperation("菜单编辑")
    @ApiImplicitParam(name = "menuDTO", value = "菜单实体类对象", required = true, defaultValue = "{}", dataTypeClass = MenuDTO.class)
    @PutMapping("/edit")
    public R edit(@Validated(Menu.Update.class) @RequestBody MenuDTO menuDTO) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuDTO,menu);
        boolean b = menuService.updateById(menu);
        return b ? R.ok().message("编辑成功") : R.error().message("编辑失败");
    }
    @WebLog(description = "菜单删除")
    @ApiOperation("菜单删除")
    @ApiImplicitParam(name = "ids", value = "菜单编号集合", required = true, defaultValue = "[]", dataTypeClass = List.class)
    @PostMapping("/remove")
    public R remove(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getParentId,ids);
        List<Menu> list = menuService.list(lambdaQueryWrapper);
        List<Integer> childrenMenuIds = list.stream().map(Menu::getId).collect(Collectors.toList());
        if(childrenMenuIds.size()!=0){
            // 先删除子菜单,再删除上级菜单
            menuService.removeByIds(childrenMenuIds);
        }
        boolean b = menuService.removeByIds(ids);
        return b ? R.ok().message("删除成功!"):R.error().message("删除失败");
    }

    @ApiOperation("根据用户角色返回菜单列表")
    @GetMapping("/queryMenuListByRole")
    public R queryMenuListByRole() {
        return menuService.queryMenuListByRole();
    }

    @ApiOperation("返回当前菜单的子菜单的最大可用编号")
    @ApiImplicitParam(name = "pid", value = "上级菜单编号", required = true, dataType = "Integer", dataTypeClass = Integer.class)
    @GetMapping("/queryChildrenMenuMaxId/{pid}")
    public R queryChildrenMenuMaxId(
            @PathVariable Integer pid) {
        Integer maxId = menuService.queryChildrenMenuMaxId(pid);
        return ObjectUtil.isNotEmpty(maxId)? R.ok().data("maxNewId",maxId):R.ok().message("菜单Id生成错误");
    }

    @ApiOperation("判断输入的根级菜单编号是否存在")
    @ApiImplicitParam(name = "menuId", value = "菜单编号", required = true, dataType = "Integer", dataTypeClass = Integer.class)
    @GetMapping("/checkMenuIdIsExisting/{menuId}")
    public R checkMenuIdIsExisting(
            @PathVariable Integer menuId) {
     return menuService.checkMenuIdIsExisting(menuId);
    }

    @ApiOperation("判断菜单标题是否重复")
    @ApiImplicitParam(name = "title", value = "菜单标题", required = true, dataType = "String", dataTypeClass = String.class)
    @GetMapping("/checkIsRepeatForMenuTitle/{title}")
    public R checkIsRepeatForMenuTitle(
            @PathVariable String title) {
        return menuService.checkIsRepeatForMenuTitle(title);
    }

}
